using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using uk.co.tonyditchfield.utility.logging;

namespace uk.co.tonyditchfield.tort.database
{
    public static class DBClient
    {
        private static ILog logger = new Log();
        private static IDbConnection connection;
        
        /// <summary>
        /// The connection for all database usage.
        /// <para>DBEngine and the pertinent connection string MUST be specified in app.config or web.config</para>
        /// <para>Valid entries/engines are: SqlServer2005, SqlServer2000, Oracle9, Oracle10 so far...</para>
        /// <para>An example follows below:</para>
        /// <para>&lt;add key="DBEngine" value="SqlServer2005"/&gt;</para>
        ///	<para>&lt;add key="DBConnection" value="Persist Security Info=False;Integrated Security=SSPI;database=MyDB;server=(local);"/&gt;</para>
        /// </summary>
        /// <returns>A generic DbConnection</returns>
        /// <exception cref="PersistenceException"></exception>
        public static DbConnection Connection()
        {
            switch (providers.ConnectionAttributes.ServerDetails().Engine)
            {
                case uk.co.tonyditchfield.tort.database.engine.DBEngine.SqlServer2005:
                    connection = providers.SQLClient.Connection();
                    break;
                case uk.co.tonyditchfield.tort.database.engine.DBEngine.SqlServer2000:
                    connection = providers.SQLClient.Connection();
                    break;
                case uk.co.tonyditchfield.tort.database.engine.DBEngine.Oracle9i:
                    connection = providers.OracleClient.Connection();
                    break;
                case uk.co.tonyditchfield.tort.database.engine.DBEngine.Oracle10g:
                    connection = providers.OracleClient.Connection();
                    break;
            }
            if (connection != null)
            {
                return (DbConnection)connection;
            }
            else
            {
                logger.LogError("Invalid Engine Specified");
                throw new exception.PersistenceException("Invalid Engine Specified");
            }
        }


        /// <summary>
        /// Generic reader based on connection above. (Testing only at this stage!!)
        /// </summary>
        /// <param name="sql">The sql to execute</param>
        /// <returns>DbDataReader</returns>
        public static IDataReader ExecuteRead(string sql)
        {
            IDbCommand command = null;
            IDataReader reader = null;
            try
            {
                command = Connection().CreateCommand();
                command.Connection.Open();
                command.CommandText = sql;
                reader = command.ExecuteReader();
                return reader;
            }
            catch (DbException dbe)
            {
                logger.LogError(dbe);
                throw new exception.PersistenceException(dbe.ErrorCode, dbe);
            }
            finally
            {
                utility.DBUtility.CleanUp(command.Connection, reader);
            }
        }
    }
}
